﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IMDTO;
using IMBL;
using Ext.Net;

namespace IMPPFront.Management
{
    public partial class KRMeta : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<IMDTORefCodeDetail> resultaten = IMBLMetadata.GetAllKernresultaatReferenties();
            List<IMDTORefCodeDetail> taken = IMBLMetadata.GetAllKernresultaatTaken();
            List<IMDTORefCodeDetail> gebieden = IMBLMetadata.GetAllKernresultaatGebieden();
            List<IMDTOKernResultaat> kernresultaten = IMBLMetadata.GetAllKernresultaten();

            KRStore.DataSource = kernresultaten;
            KRStore.DataBind();
            KRRStore.DataSource = resultaten;
            KRRStore.DataBind();
            KRRCombo.DataSource = resultaten;
            KRRCombo.DataBind();
            KTStore.DataSource = taken;
            KTStore.DataBind();
            KTCombo.DataSource = taken;
            KTCombo.DataBind();
            KRGStore.DataSource = gebieden;
            KRGStore.DataBind();
            KRGCombo.DataSource = gebieden;
            KRGCombo.DataBind();

        }

        protected void HandleChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            ChangeRecords<IMDTOKernResultaat> competenties = e.DataHandler.ObjectData<IMDTOKernResultaat>();

            foreach (IMDTOKernResultaat created in competenties.Created)
            {
                int tempId = created.IdKernresultaat;
                int newId = IMBLMetadata.AddKernresultaat(created);
                if (KRStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(tempId.ToString(), newId.ToString());
                }
                else
                {
                    KRStore.UpdateRecordId(tempId, newId);
                }

            }

            foreach (IMDTOKernResultaat deleted in competenties.Deleted)
            {
                IMBLMetadata.DeleteKernresultaat(deleted.IdKernresultaat);

                if (KRStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(deleted.IdKernresultaat.ToString());
                }
            }

            foreach (IMDTOKernResultaat updated in competenties.Updated)
            {
                IMBLMetadata.UpdateKernresultaat(updated);

                if (KRStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(updated.IdKernresultaat.ToString());
                }
            }
            e.Cancel = true;
        }

        public void HandleRefCodeChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            ChangeRecords<IMDTORefCodeDetail> refcodes = e.DataHandler.ObjectData<IMDTORefCodeDetail>();
            Store senderStore = (Store)sender;

            foreach (IMDTORefCodeDetail created in refcodes.Created)
            {
                int tempId = created.idRefCodeDetail;
                int newId = IMBLMetadata.AddRefCodeDetail(created);
                if (senderStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(tempId.ToString(), newId.ToString());
                }
                else
                {
                    senderStore.UpdateRecordId(tempId, newId);
                }

            }

            foreach (IMDTORefCodeDetail deleted in refcodes.Deleted)
            {
                IMBLMetadata.DeleteRefCodeDetail(deleted.idRefCodeDetail);

                if (senderStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(deleted.idRefCodeDetail.ToString());
                }
            }

            foreach (IMDTORefCodeDetail updated in refcodes.Updated)
            {
                IMBLMetadata.UpdateRefCodeDetail(updated);

                if (senderStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(updated.idRefCodeDetail.ToString());
                }
            }
            e.Cancel = true;
        }

        public void KRRStore_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            List<IMDTORefCodeDetail> resultaten = IMBLMetadata.GetAllKernresultaatReferenties();
            KRRStore.DataSource = resultaten;
            KRRStore.DataBind();
            KRRCombo.DataSource = resultaten;
            KRRCombo.DataBind();
        }

        public void KRGStore_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            List<IMDTORefCodeDetail> gebieden = IMBLMetadata.GetAllKernresultaatGebieden();
            KRGStore.DataSource = gebieden;
            KRGStore.DataBind();
            KRGCombo.DataSource = gebieden;
            KRGCombo.DataBind();
        }

        public void KTStore_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            List<IMDTORefCodeDetail> taken = IMBLMetadata.GetAllKernresultaatTaken();
            KTStore.DataSource = taken;
            KTStore.DataBind();
            KTCombo.DataSource = taken;
            KTCombo.DataBind();
        }
    }
}